<template>
	<view class="content">
		<image src="../../static/common/aui-icon-close.png" class="close" @tap.stop="closed"></image>
		<!-- #ifdef H5 || APP-PLUS -->
		<view class="message-title">
			<commonTitle title="绑定手机号" :isback="isback"></commonTitle>
		</view>
		
		<!-- #endif -->
		<view class="list-scroll-content">
			<view class="input-box">
				<input class="input-text" type="number" placeholder="输入手机号" v-model="phone"/>
			</view>
			<view class="input-box vcode">
				<input class="input-text" type="text" placeholder="请输入验证码" v-model="vcode"/>
				<text class="send-code" @tap="getCode" v-if="daojishi">发送验证码</text>
				<text class="send-code" v-else>{{vtext}}</text>
			</view>
			<view class="register-box">
				<view class="bind" @tap="login">立即绑定</view>
			</view>
			
			
			
			
		</view>
		
	</view>
</template>

<script>
	import commonTitle from'../../components/titile/title.vue'
	export default{
		components:{
			commonTitle
		},
		data () {
			return {
				apptoken:"",
				userId:"",
				relation_id:"",
				phone:"",
				vcode:"",
				vtext:"获取验证码",
				submit:false,
				type:1,//1为密码登录，2为验证码登录
				typeText:"手机验证码登录",
				timer:"timer",
				daojishi:true,
				isback:false
			}
		},
		onLoad(option) {
			this.apptoken = option.apptoken;
			this.userId = option.userId;
			this.relation_id = option.relation_id;
		},
		methods: {
			closed(e){
				uni.showModal({
					title:"温馨提示",
					content:"您确定要退出登录吗?",
					success(res) {
						if(res.confirm){
							//清除登录token
						
							// uni.removeStorage({
							// 	key:"UserInfo",
							// })
							// uni.removeStorage({
							// 	key:"inviteId"
							// })
							// uni.removeStorage({
							// 	key:"userId"
							// })
							uni.switchTab({
								url:'../home/home'
							})
							// uni.removeStorage({
							// 	key:"apptoken",
							// 	success() {
							// 		uni.switchTab({
							// 			url:'../home/home'
							// 		})
							// 	}
							// })
						}
					}
				})
				
			},
			getCode(){
				
				if(!this.phone){
					this.$CommonJS.showToast("请输入手机号");
					return;
				}
				if(!(/^1[3456789]\d{9}$/.test(this.phone))){
					this.$CommonJS.showToast("手机号码有误，请重填");
					return; 
				} 
				const _this = this;
				this.daojishi = !this.daojishi;
				uni.request({
					url:this.$API.URL + this.$API.phoneGetvalidatecode,
					data:{phone:this.phone},
					success(res) {
						console.log(res)
						if(res.data.success){
							_this.$CommonJS.showToast("验证码已发送");
							
							_this.getTime();
						}
					}
				})
			},
			getTime(){
				var _this = this;
				
				var num = 60;
			  var timer =	 setInterval(function(){
					if(num <= 0){
						clearInterval(timer);
						_this.vtext = "";
						_this.daojishi = true;
						return;
					}
					
					_this.vtext = '重新发送' + num + 's';
					num --;
				},1000)
			},
			login(){
				const _this = this;
				if(!this.phone){
					this.$CommonJS.showToast("请输入手机号");
					return;
				}
				if(!(/^1[3456789]\d{9}$/.test(this.phone))){
					this.$CommonJS.showToast("手机号码有误，请重填");
					return; 
				} 
				if(!this.vcode){
						this.$CommonJS.showToast("请输入验证码");
						return;
					}
					uni.request({
						url:_this.$API.URL + _this.$API.userPhone,
						data:{apptoken:this.apptoken,phone:this.phone,vcode:this.vcode},
						success(resp) {
							console.log(resp)
							if(resp.data.success){
								uni.request({
									url:_this.$API.URL + _this.$API.userView,
									data:{
										apptoken:_this.apptoken
									},
									success(res) {
											console.log(res)
											if(res.data.success){
												uni.setStorage({
													key:"UserInfo",
													data:res.data.data
												})
											}
									}
								})
								uni.showToast({
									title:"绑定成功",
									success() {
										uni.setStorage({
											key:"apptoken",
											data:_this.apptoken,
										})
										uni.setStorage({
											key:"userId",
											data:_this.userId,
										})
										uni.setStorage({
											key:"isAutoTbao",
											data:_this.relation_id,
											success() {
												uni.switchTab({
													url:'../home/home',
												})
											}
										})
									}
								})
							}else{
								uni.showToast({
									title:resp.data.msg
								})
							}
						}
					})
				
			},

		}
	}
</script>

<style>
	@import url("register.css");
</style>
